package com.project.smarthome.common.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.TextView;

import com.project.smarthome.R;


/**
 * Create custom Dialog windows for your application
 * Custom dialogs rely on custom layouts wich allow you to
 * create and use your own look & feel.
 * <p/>
 * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 * <p/>
 * <a href="http://my.oschina.net/arthor" target="_blank" rel="nofollow">@author</a> antoine vianey
 */
public class SosDialog extends Dialog {

    private MessageDialogListener mMessageDialogListener;
    private SosDialog mDialog;

    public SosDialog(Context context, int theme) {
        super(context, theme);
    }

    public SosDialog(Context context) {
        super(context);
    }

    public SosDialog getDialog(Context context, String content, String sureMsg) {
        Builder builder = new Builder(context);
        builder.setMessage(content);
        builder.setmSureMsg(sureMsg);
        mDialog = builder.create();
        if (context != null && !((Activity)context).isFinishing()) {
            mDialog.show();
        }
        return mDialog;
    }

    public void dissMissDialog() {
        if(mDialog !=null)
            mDialog.dismiss();
    }
    /**
     * Helper class for creating a custom mDialog
     */
    public class Builder {
        private Context mContext;
        private TextView txtName;
        private TextView txtContent;
        private String mTitle;
        private String mContent;
        private String mSureMsg;
        private TextView txtSure;

        public Builder(Context context) {
            this.mContext = context;
        }

        public void setMessage(String content) {
            this.mContent = content;
        }

        public void setmSureMsg(String msg) {
            this.mSureMsg = msg;
        }

        /**
         * Create the custom mDialog
         */
        @SuppressLint("Override")
        public SosDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the mDialog with the custom Theme
            final SosDialog dialog = new SosDialog(mContext,
                    R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);
            View layout = inflater.inflate(R.layout.view_remind_no_titile_dialog_sos, null);
            txtContent = (TextView) layout.findViewById(R.id.txt_content);
            txtSure = (TextView) layout.findViewById(R.id.txt_sure);
            txtContent.setText(mContent);
            if (!TextUtils.isEmpty(mSureMsg)) {
                txtSure.setText(mSureMsg);
            }
            layout.findViewById(R.id.txt_sure).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mMessageDialogListener != null) {
                        mMessageDialogListener.sure();
                    }
                    dialog.dismiss();
                }
            });
            dialog.addContentView(layout, new LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);
            return dialog;
        }
    }

    public interface MessageDialogListener {
        void sure();
    }

    public void seteditDialogListener(MessageDialogListener messageDialogListener) {
        this.mMessageDialogListener = messageDialogListener;
    }
}